package com.siemens.industrial.web.mvc.service;

import com.siemens.industrial.web.mvc.dao.admin.ChartsDao;
import com.siemens.industrial.web.mvc.dao.partner.PartnerChartsDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shuan on 2016/12/30.
 */
@Service
public class ExportDataFormatService {
    @Resource
    ChartsDao chartsDao;

    @Resource
    PartnerChartsDao partnerChartsDao;

    //admin oee
    public List<Map<String, Object>> dataFormatForAdminOeeExport(List<Map<String, Object>> list, List<Map<String, Object>> paramList) {
        if (paramList.size() == 0)
            return new ArrayList<>();

        Map<String, Object> mtidMap = new HashMap<>();
        for (Map<String, Object> map : paramList) {
            String mtid = map.get("deviceName").toString();
            if (!mtidMap.containsKey(mtid)) {
                mtidMap.put(mtid, 0);
            }
        }

        Map<String, List<Map<String, Object>>> temporary = new HashMap<>();
        for (Map<String, Object> oeeMap : list) {
            String mtid = oeeMap.get("deviceName").toString();
            List<Map<String, Object>> sheet;
            if (mtidMap.get(mtid).equals(0)) {
                sheet = new ArrayList<>();
            } else {
                sheet = temporary.get(mtid);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("date", oeeMap.get("date_id"));
            map.put("oee", oeeMap.get("oee"));
            sheet.add(map);
            temporary.put(mtid, sheet);
            mtidMap.put(mtid, Integer.parseInt(mtidMap.get(mtid).toString()) + 1);
        }

        List<Map<String, Object>> allSheet = new ArrayList<>();

        for (Map<String, Object> map : paramList) {
            String mtid = map.get("deviceName").toString();
            Map<String, Object> sheet = new HashMap<>();
            sheet.put("mtid", mtid);
            sheet.put("values", temporary.get(mtid));
            allSheet.add(sheet);
        }
        return allSheet;
    }

    //partner oee
    public List<Map<String,Object>> dataFormatForPartnerOeeExport(List<Map<String,Object>> list,String[] deviceIdArr,String type){
        List<Map<String,Object>> allSheet = new ArrayList<>();
        for (String deviceId:deviceIdArr) {
            Map<String,Object> sheet = new HashMap<>();
            List<Map<String,Object>> sheetData = new ArrayList<>();
            for (Map<String, Object> deviceData:list){
                if(deviceId.equals(deviceData.get("device_id"))){
                    Map<String,Object> value = new HashMap<>();
                    value.put("date",deviceData.get("date_id"));
                    value.put(type,deviceData.get(type));
                    sheetData.add(value);
                    if(!sheet.containsKey("mtid")){
                        sheet.put("mtid",deviceData.get("deviceName"));
                    }
                }
            }
            sheet.put("values",sheetData);
            allSheet.add(sheet);
        }
        return allSheet;
    }

    //shift oee
    public List<Map<String, Object>> dataFormatAdminShiftOee(ArrayList<Map<String, Object>> tenantAndDevice, String day, String type) {
        try {
            List<Map<String, Object>> allSheet = new ArrayList<>();

            List<Map<String, Object>> serviceList = chartsDao.findShiftServiceNew(tenantAndDevice, day);
            List<Map<String, Object>> devices = chartsDao.findDeviceNameById(tenantAndDevice);
            if (serviceList.size() == 0) {
                return allSheet;
            }
            allSheet = shiftOeeFormat(serviceList, devices, type);

            return allSheet;
        } catch (Exception e) {
            System.out.print(e);
            return null;
        }
    }

    //partner shift oee
    public List<Map<String, Object>> getServicePartnerShiftOee(String database, String[] deviceIdArr, String day, String type) {
        try {
            List<Map<String, Object>> allSheet = new ArrayList<>();
            //查询某服务 日期是否在服务期限内
            List<Map<String, Object>> isInRange = chartsDao.findDayTenantServiceStateByType(database, day, type);
            if (isInRange.size() == 0 || Integer.parseInt(isInRange.get(0).get("inrange").toString()) != 1) {
                return allSheet;
            }
            List<Map<String, Object>> serviceList = partnerChartsDao.findShiftServiceByNew(database, deviceIdArr, day);
            List<Map<String, Object>> devices = partnerChartsDao.findDeviceNameById(database, deviceIdArr);

            allSheet = shiftOeeFormat(serviceList, devices, type);
            return allSheet;
        } catch (Exception e) {
            System.out.print(e);
            return null;
        }
    }

    //admin or partner shift oee etc dataFormat
    public List<Map<String, Object>> shiftOeeFormat(List<Map<String, Object>> serviceList, List<Map<String, Object>> devices, String type) {
        List<Map<String, Object>> allSheet = new ArrayList<>();

        for (Map<String, Object> device : devices) {
            Map<String, Object> row = new HashMap<>();
            ArrayList<Map<String, Object>> values = new ArrayList<>();

            for (Map<String, Object> service : serviceList) {
                if (service.get("deviceName").equals(device.get("deviceName"))) {
                    Map<String, Object> value = new HashMap<>();
                    value.put("date", service.get("shift_start_time") + "~" + service.get("shift_end_time"));
                    value.put(type, service.get(type));
                    values.add(value);
                }
            }
            row.put("mtid", device.get("deviceName"));
            row.put("values", values);
            allSheet.add(row);
        }
        return allSheet;
    }

    public List dataFormatForSameReasonExport(List<String> MTids, List<Map<String, Object>> datas) {
        for (String mtid : MTids) {
            for (Map<String, Object> map : datas) {
                if (!map.containsKey(mtid)) {
                    map.put(mtid, "0");
                }
            }
        }
        return datas;
    }

    public List dataFormatForDiffReasonExport(List<String> reasons, List<Map<String, Object>> datas) {
        for (String reason : reasons) {
            for (Map<String, Object> map : datas) {
                if (!map.containsKey(reason)) {
                    map.put(reason, "0");
                }
            }
        }
        return datas;
    }

    //admin and partner free time dataFormat
    public List<Map<String,Object>> shiftFreeTimeDataFormat(String deviceName,String day,String startTime,String endTime,List<Map<String,Object>> list){
        List<Map<String,Object>> sheets = new ArrayList<>();
        Map<String,Object> sheet = new HashMap<>();
        for (Map<String ,Object> reason:list) {
            reason.put("shift",day+" "+startTime+"~"+day+" "+endTime);
            reason.put("time",reason.get("time")+"秒");
        }
        sheet.put("mtid",deviceName);
        sheet.put("values",list);
        sheets.add(sheet);
        return sheets;
    }

    //admin adn partner shiftStates dataFormat
    public List<Map<String,Object>> dayOrShiftAdminState(String deviceName,String day,String startTime,String endTime,List<Map<String,Object>> list){

        List<Map<String,Object>> sheets = new ArrayList<>();
        Map<String,Object> sheet = new HashMap<>();
        List<Map<String,Object>> values = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();

        map.put("periodTime",day+" "+startTime+"~"+day+" "+endTime);
        for (Map<String ,Object> state:list) {
            if(state.get("state").equals(0)){
                map.put("offLine",state.get("time")+"秒");
            }else if (state.get("state").equals(1)){
                map.put("onLine",state.get("time")+"秒");
            }
        }

        values.add(map);
        sheet.put("mtid",deviceName);
        sheet.put("values",values);
        sheets.add(sheet);
        return sheets;
    }

    //admin and partner device state dataFormat
    public List<Map<String,Object>> deviceStateFormat(List<Map<String,Object>> devices,List<Map<String, Object>> dataSource){
        List<Map<String,Object>> sheets = new ArrayList<>();
        for (Map<String,Object> device:devices){

            Map<String,Object> sheet = new HashMap<>();
            List<Map<String,Object>> values = new ArrayList<>();

            for (Map<String,Object> data:dataSource){
                if(data.containsKey(device.get("deviceId")+"on")){
                    Map<String,Object> value = new HashMap<>();
                    value.put("date",data.get("date_id"));
                    value.put("on",data.get(device.get("deviceId")+"on"));
                    value.put("off",data.get(device.get("deviceId")+"off"));
                    values.add(value);
                }
            }
            sheet.put("mtid",device.get("deviceName"));
            sheet.put("values",values);
            sheets.add(sheet);
        }
        return sheets;
    }
}
